package com.ask.javabagubooster.a_java_basis.juc;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SyncUnorderedPrinter {
    private int counter = 1;
    private final Object lock = new Object();
    private int limit = 100;

    public void print() {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);


        for (int i = 0; i < 5; i++) {
            threadPool.submit(() -> {
                while (true) {
                    int curNum;
                    synchronized (lock) {
                        if (counter > limit) {
                            break;
                        }
                        curNum = counter;
                        counter++;

                    }
                    System.out.println(Thread.currentThread().getName() + "prints:" + curNum);
                }
            });
        }


    }


}
